package cn.fanqinghong.basic.util;

import cn.fanqinghong.org.domain.Employee;
import cn.fanqinghong.org.domain.ShopAuditLog;
import cn.fanqinghong.org.mapper.EmployeeMapper;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * FAN
 * 发送邮件
 * 2022/6/13  18:54
 */
public class MailUtil {

    private static JavaMailSender javaMailSender;
    private static EmployeeMapper employeeMapper;

    /**
     * 发送审核通过的邮件
     * @param log
     */
    public static void sendEasyMailPass(ShopAuditLog log){
        try {
            javaMailSender = ApplicationContextUtils.getBean(JavaMailSender.class);
            employeeMapper = ApplicationContextUtils.getBean(EmployeeMapper.class);
            Employee employee = employeeMapper.loadByShopId(log.getShopId());
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //3.发送激活邮件

            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("1412644233@qq.com");
            message.setTo(employee.getEmail());
            message.setSubject("宠物乐园-店铺入驻激活请求");
            message.setText("亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
                    "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
            javaMailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送审核驳回的邮件
     * @param log
     */
    public static void sendEasyMailReject(ShopAuditLog log){
        try {
            javaMailSender = ApplicationContextUtils.getBean(JavaMailSender.class);
            employeeMapper = ApplicationContextUtils.getBean(EmployeeMapper.class);
            Employee employee = employeeMapper.loadByShopId(log.getShopId());
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //3.发送激活邮件
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("1412644233@qq.com");
            message.setTo(employee.getEmail());
            message.setSubject("宠物乐园-店铺入驻激活请求");
            message.setText("亲，您的店铺信息已经驳回，请重新填写内容，修改链接：" +
                    "http://localhost:8081/#/register2?shopId="+log.getShopId());
            javaMailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送复杂邮件
     */
    public static void sendMultipartMail(){
        try {
            javaMailSender = ApplicationContextUtils.getBean(JavaMailSender.class);
            employeeMapper = ApplicationContextUtils.getBean(EmployeeMapper.class);
            //创建复杂邮件对象
            MimeMessage mimeMessage = MailUtil.javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1412644233@qq.com");
            helper.setSubject("邮件标题");
            //第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>新型冠状病毒防护守则</h1>"+
                    "<img src='http://img30.360buyimg.com/5db64acfE6ab2b09e/38b5cb3dc38b4b1f.jpg\"' />",true);
            //添加附件
            helper.addAttachment("文件名.jpg",new File("C:\\aa.jpg"));
            //收件人
            helper.setTo("1412644233@qq.com");
            //发送邮件
            MailUtil.javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
